PROJECT (tools23d)

INCLUDE (CPack)

# Need 2.6 for SDL to be included without separately adding SDLmain.a
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)

MESSAGE (STATUS "Entered ${PROJECT} CMakeLists.txt")

SET(CurrentProject ${CMAKE_PROJECT_NAME})
SET(BTime_Executable "btime23d")
SET(Photo_Executable "photo23d")

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR} ./)
INCLUDE_DIRECTORIES ( ${INCLUDE_DIRECTORIES} ${CMAKE_SOURCE_DIR}/include )
FIND_PACKAGE ( FreeImage REQUIRED )
FIND_PACKAGE ( SDL2 )

IF (NOT FREEIMAGE_FOUND)
	MESSAGE ( FATAL_ERROR "Unable to find FreeImage library." )
ENDIF (NOT FREEIMAGE_FOUND)

IF (NOT SDL2_FOUND)
	MESSAGE ( WARNING "Unable to find SDL2 library - preview functionality will not be built." )
ENDIF (NOT SDL2_FOUND)

IF (FREEIMAGE_LIBRARY_DEBUG STREQUAL "")
	SET (FREEIMAGE_LIBRARY_DEBUG ${FREEIMAGE_LIBRARY})
ENDIF (FREEIMAGE_LIBRARY_DEBUG STREQUAL "")

SET ( INCLUDED_LIBRARIES ${INCLUDED_LIBRARIES} ${FREEIMAGE_LIBRARY} )
INCLUDE_DIRECTORIES ( ${INCLUDE_DIRECTORIES} ${FREEIMAGE_INCLUDE_DIR} )

ADD_DEFINITIONS ( -DFREEIMAGE_LIB )

IF (WIN32)
	SET (GLOB_PLATFORM_SOURCE "./src/globule/glob-windows.c")
	SET (SYSTEM_PLATFORM_SOURCE "./src/system/system-windows.c")
	
	IF (MSVC)
		# SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:MSVCRTD.lib")
		SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:LIBCMTD.lib")
	ENDIF (MSVC)
ELSE (WIN32)
	SET (GLOB_PLATFORM_SOURCE "./src/globule/glob-posix.c")
	SET (SYSTEM_PLATFORM_SOURCE "./src/system/system-posix.c")
ENDIF (WIN32)

SET (CPACK_DEBIAN_PACKAGE_MAINTAINER "dgorst@gmail.com")

# From this excellent webpage : http://majewsky.wordpress.com/2010/08/14/tip-of-the-day-cmake-and-doxygen/
# Add a target to generate API documentation with Doxygen
FIND_PACKAGE(Doxygen)
IF(DOXYGEN_FOUND)
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
	ADD_CUSTOM_TARGET(doc ALL
		${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
		COMMENT "Generating API documentation with Doxygen" VERBATIM
	)
ELSE (DOXYGEN_FOUND)
	MESSAGE ( WARNING "Doxygen not found. Documentation build target not
added.")
ENDIF(DOXYGEN_FOUND)

# Main build details.

SET(BTime_SRCS
	# Sources
	./src/main/btime23d.c
	./src/main/tools23d.c
	./src/ui/cli.c
	./src/ui/cli-btime.c
	./src/ui/cli-common.c
	./src/image/image.c
	./src/image/image-calc.c
	./src/globule/glob-all.c
	./src/main/frameset.c
	./src/util/readml.c
	./src/util/util.c
	./src/util/list.c
	./src/image/image-fi.c
	${GLOB_PLATFORM_SOURCE}
	${SYSTEM_PLATFORM_SOURCE}

	# Headers
	./include/tools23d.h
	./include/btime23d.h
	./include/ui.h
	./include/cli.h
	./include/cli-common.h
	./include/cli-btime.h
	./include/glob-all.h
	./include/image.h
	./include/image-calc.h
	./include/frameset.h
	./include/readml.h
	./include/util.h
	./include/list.h
	./include/image-fi.h
	./include/system.h
)

SET(Photo_SRCS
	# Sources
	./src/main/photo23d.c
	./src/main/tools23d.c
	./src/ui/cli.c
	./src/ui/cli-photo.c
	./src/ui/cli-common.c
	./src/image/image.c
	./src/image/image-calc.c
	./src/globule/glob-all.c
	./src/main/frameset.c
	./src/util/readml.c
	./src/util/util.c
	./src/util/list.c
	./src/image/image-fi.c
	${GLOB_PLATFORM_SOURCE}
	${SYSTEM_PLATFORM_SOURCE}
	
	# Headers
	./include/tools23d.h
	./include/photo23d.h
	./include/ui.h
	./include/cli.h
	./include/cli-common.h
	./include/cli-photo.h
	./include/glob-all.h
	./include/image.h
	./include/image-calc.h
	./include/frameset.h
	./include/readml.h
	./include/util.h
	./include/list.h
	./include/image-fi.h
	./include/system.h
)

ADD_EXECUTABLE(${BTime_Executable} ${BTime_SRCS} )
ADD_EXECUTABLE(${Photo_Executable} ${Photo_SRCS} )

SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES COMPILE_FLAGS -DBUILD_BTIME)
SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES COMPILE_FLAGS -DBUILD_PHOTO)

IF (MSVC)
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES COMPILE_DEFINITIONS_RELEASE "_CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${BTime_Executable} PROPERTIES COMPILE_DEFINITIONS_MINSIZEREL "_CONSOLE")

	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES COMPILE_DEFINITIONS_RELEASE "_CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE")
	SET_TARGET_PROPERTIES(${Photo_Executable} PROPERTIES COMPILE_DEFINITIONS_MINSIZEREL "_CONSOLE")
ENDIF(MSVC)

IF (NOT FREEIMAGE_LIBRARY_DEBUG)
	MESSAGE ( WARNING " Cannot find debug FreeImage lib." )
ELSE (NOT FREEIMAGE_LIBRARY_DEBUG)

	TARGET_LINK_LIBRARIES (
        ${BTime_Executable}
        debug ${FREEIMAGE_LIBRARY_DEBUG}
	)

	TARGET_LINK_LIBRARIES (
        ${BTime_Executable}
		general ${FREEIMAGE_LIBRARY_DEBUG}
	)

	TARGET_LINK_LIBRARIES (
        ${Photo_Executable}
        debug ${FREEIMAGE_LIBRARY_DEBUG}
	)

	TARGET_LINK_LIBRARIES (
        ${Photo_Executable}
		general ${FREEIMAGE_LIBRARY_DEBUG}
	)

ENDIF (NOT FREEIMAGE_LIBRARY_DEBUG)

IF (NOT FREEIMAGE_LIBRARY)
	MESSAGE ( WARNING " Cannot find release FreeImage lib." )
ELSE (NOT FREEIMAGE_LIBRARY)
    MESSAGE ( INFO " Linking with FreeImage lib ${FREEIMAGE_LIBRARY}." )
	TARGET_LINK_LIBRARIES (
        ${BTime_Executable}
		optimized ${FREEIMAGE_LIBRARY}
	)

	TARGET_LINK_LIBRARIES (
        ${Photo_Executable}
        optimized ${FREEIMAGE_LIBRARY}
	)

ENDIF (NOT FREEIMAGE_LIBRARY)

set(CPACK_GENERATOR NSIS)
set(CPACK_PACKAGE_NAME "Tools23D")
set(CPACK_PACKAGE_VENDOR "Dave Gorst")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Tools to convert 2D images to 3D.")
set(CPACK_PACKAGE_VERSION "0.0.1")
set(CPACK_PACKAGE_VERSION_MAJOR "0")
set(CPACK_PACKAGE_VERSION_MINOR "0")
set(CPACK_PACKAGE_VERSION_PATCH "1")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "tools23d")
SET(CPACK_NSIS_MODIFY_PATH ON)
